package drive.Mapper;

import drive.Model.Image;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ImageMapper {

    @Select("SELECT * FROM image")
    List<Image> findAll();

    @Select("SELECT * FROM image WHERE imageID = #{imageID}")
    Image findByImageID(Integer imageID);

    @Select("SELECT * FROM image WHERE articleID = #{articleID}")
    List<Image> findByArticleID(Integer articleID);

    @Insert("INSERT INTO image (imageUrl, articleID, indexNo, uploadTime, imgType) VALUES (#{imageUrl}, #{articleID}, #{indexNo}, #{uploadTime}, #{imgType})")
    @Options(useGeneratedKeys = true, keyProperty = "imageID", keyColumn = "imageID")
    int add(Image image);

    @Update("UPDATE image SET imageUrl = #{imageUrl}, articleID = #{articleID}, indexNo = #{indexNo}, uploadTime = #{uploadTime} WHERE imageID = #{imageID}")
    int update(Image image);

    @Delete("DELETE FROM image WHERE imageID = #{imageID}")
    int delete(Integer imageID);

    /**
     * 根据商品ID获取第一张图片
     * @param itemID 商品ID
     * @return 图片对象
     */
    @Select("SELECT * FROM image WHERE articleID = #{itemID} AND imgType = #{imgType} ORDER BY indexNo ASC LIMIT 1")
    Image getFirstImageByItemID(int itemID,String imgType);

    @Select("SELECT * FROM image WHERE articleID = #{itemID} AND imgType = #{imgType} ORDER BY indexNo ASC")
    List<Image> getImageByItemID(int itemID,String imgType);
}